package com.rimi.lzhg.controller;

import com.rimi.lzhg.entry.Order;
import com.rimi.lzhg.response.ResponseEntity;
import com.rimi.lzhg.service.MenuService;
import com.rimi.lzhg.service.OrderService;
import com.rimi.lzhg.service.TodayMenuService;
import com.rimi.lzhg.util.DateUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins="*")
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private TodayMenuService todayMenuService;

    @Autowired
    private MenuService menuService;

    @ApiOperation(value = "查询订单列表",notes = "查询所有订单信息 可根据用户ID（userId）进行查询")
    @GetMapping("/order")
    private Object getList(Order order){
       return orderService.getOrderList(order);
    }

    @ApiOperation(value = "查询我的订单",notes = "查询我的订单信息 根据用户ID（userId）进行查询")
    @GetMapping("/myorder")
    private Object myOrder(Integer userId){
        if(userId==null){
            return ResponseEntity.error("未传递用户ID");
        }
        Order o=new Order(userId);
        //根据用户Id查询订单表
        List<Order> orderList = orderService.getOrderList(o);
        Map<String,Map<String,List>> map=new HashMap();
        //按照日期添加所有的晚餐
        for (Order order : orderList) {
            Map m=new HashMap();
            //晚餐
            if(order.getType()==2){
                m.put("dinner",order.getMenuList());
                map.put(DateUtil.dateToString2(order.getCreateTime()),m);
            }
        }
        //检查是否有同日期的午餐
        for (Order order : orderList) {
            Map m=new HashMap();
            //午餐
            if(order.getType()!=2){
                String date = DateUtil.dateToString2(order.getCreateTime());
                Map<String, List> stringListMap = map.get(date);
                //有重复
                if(stringListMap!=null){
                    stringListMap.put("lunch",order.getMenuList());
                }else{
                    //未重复
                    m.put("lunch",order.getMenuList());
                    map.put(date,m);
                }
            }
        }
        return map;
    }


    @ApiOperation(value = "添加订单",notes = "添加订单信息  时间默认当前时间")
    @PostMapping("/order")
    public Object saveOrder(@RequestBody Order order){
        return orderService.saveOrder(order);
    }

}
